package gui.components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import network.Message;
import network.MessageFactory;
import controller.NetworkController;
import data.ChatMessages;
import data.WorkingMode;

/**
 * Chat panel is a gui component which is used for user to input chat message
 * and display chat messages.
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class ChatPanel extends JPanel implements KeyListener, Observer {

	JTextArea chatArea = new JTextArea();
	JPanel inputPanel = new JPanel();

	JLabel messageLabel = new JLabel();
	JTextField chatInput = new JTextField();
	JButton sendButton = new JButton();
	JPanel chatPanel = new JPanel();
	JScrollPane chatScroll = new JScrollPane(chatArea);

	public ChatPanel() {
		ChatMessages.getInstance().addObserver(this);
		this.setLayout(new BorderLayout());
		this.add(inputPanel, BorderLayout.SOUTH);
		final FlowLayout flowLayout_1 = new FlowLayout();
		flowLayout_1.setAlignment(FlowLayout.LEADING);
		inputPanel.setLayout(flowLayout_1);

		inputPanel.add(messageLabel);
		messageLabel.setText("Message:");

		inputPanel.add(chatInput);
		chatInput.setColumns(65);

		inputPanel.add(sendButton);
		sendButton.addActionListener(new SendButtonActionListener());
		// sendButton.addActionListener(new SendButtonActionListener());
		sendButton.setText("Send");

		chatPanel.setAutoscrolls(true);
		chatPanel.setLayout(new BorderLayout());
		this.add(chatPanel);

		chatPanel.add(chatScroll);

		chatArea.setRows(6);
		chatArea.setEditable(false);
		chatInput.addKeyListener(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		try {
			data.ChatMessages.Message msg = (data.ChatMessages.Message) arg;
			addChat(msg.sender, msg.msg);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {

	}

	@Override
	public void keyTyped(KeyEvent e) {
		if (e.getKeyChar() == KeyEvent.VK_ENTER) {
			sendButton_actionPerformed(null);
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void addChat(String sender, String message) {
		chatArea.append(sender + " says:" + message
				+ System.getProperty("line.separator"));
	}

	private class SendButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			sendButton_actionPerformed(e);
		}
	}

	protected void sendButton_actionPerformed(ActionEvent e) {
		String msg = chatInput.getText();
		if (msg == null || msg.equals(""))
			return;
		switch (WorkingMode.getInstance().getWorkingMode()) {
		case SERVER:
		case CLIENT:
			Message m = MessageFactory.createChatMessage(NetworkController
					.getInstance().getUsername(), msg);
			NetworkController.getInstance().sendMessage(m);

			break;
		case LOCAL:
			addChat("Me", chatInput.getText());
			break;
		}
		chatInput.setText("");
	}
}
